package fr.lelouet.monitoring.snapshot.receivers;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 * a simple test to add values to several graphs. Each graph is identified by an
 * id,
 */
public class GraphTester extends JPanel {

	private static final long serialVersionUID = 1;

	protected int nb_graphs = 5;

	public int nbGraphs() {
		return nb_graphs;
	}

	protected TimeSeries[] seriess;

	public TimeSeries getSeries(int num) {
		if (num < 0)
			return null;
		if (num >= nb_graphs)
			return null;
		return seriess[num];
	}

	public GraphTester() {
		organize();
		createSeries();
		generateGraphs();
	}

	protected void organize() {
		setLayout(new GridLayout(0, 1));
	}

	protected void createSeries() {
		seriess = new TimeSeries[nb_graphs];
		for (int i = 0; i < nb_graphs; i++) {
			TimeSeries series = new TimeSeries("graph " + i);
			series.setMaximumItemAge(15);
			seriess[i] = series;
		}
	}

	protected void generateGraphs() {
		for (int i = 0; i < nb_graphs; i++) {
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			dataset.addSeries(seriess[i]);
			JFreeChart chart = ChartFactory.createXYLineChart(
					"char for series " + i, "X", "Y", dataset,
					PlotOrientation.VERTICAL, true, true, false);
			add(new ChartPanel(chart));
		}
	}

	public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame();

		GraphTester panel = new GraphTester();
		frame.setContentPane(panel);

		frame.pack();
		frame.setVisible(true);

		int counter = 0;
		while (true) {
			for (int i = 0; i < panel.nbGraphs(); i++) {
				float val = 0;
				switch (i) {
				case 0:
					val = panel.nbGraphs();
					break;
				case 1:
					val = counter % panel.nbGraphs();
					break;
				case 2:
					val = counter / panel.nbGraphs();
					break;
				default:
					val = counter % panel.nbGraphs();
				}
				panel.getSeries(i).add(new Second(), val);
				Thread.sleep(200);
			}
			counter++;
		}
	}

}
